import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBApplication.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBWeChatPopPage/YBCameraSelectPopPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBWeChatPopPage/YBImageEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBImageStyle/YBImageStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/yb_chinese_text/yb_chinese_text.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBMineModel/YBMineOrginzationManagerModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBMineProvider/YBMineUserProvider.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBMineUserInfoPage/YBMineUserInfoEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBMineUserInfoPage/YBMineWxNumberEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBAPi/YBAPi.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBBaseMineNetWork/YBBaseMineNetWork.dart';


class YBMineUserInfoMainPage extends StatefulWidget {
  @override
  _YBMineUserInfoMainPageState createState() => _YBMineUserInfoMainPageState();
}

class _YBMineUserInfoMainPageState extends State<YBMineUserInfoMainPage> {


  String _avatorStr = "";
  String _wxQrCode = '';
  bool _wxBool = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    imageStrEventBus.on<YBImageEventBus>().listen((event) {
       _avatorStr = "${YBApi.Base_image_URL}/${event.imageStr}";
       _avatorInfo(_avatorStr);
    });

    mineWxNumberEventBus.on<YBMineWxNumberEventBus>().listen((event) {
      _wxQrCode = "{YBApi.Base_image_URL}/${event.wxNumberCode}";
      _avatorInfo(event.wxNumberCode);
    });


  }


  void _avatorInfo(String editText){
    Map<String, dynamic> editingMap = Map();
    editingMap['sysCode'] = "xg_youbang";
    if (_wxBool == false) {
      editingMap['avatarUrl'] = editText;
    }else{
      editingMap['wxQrCode'] = editText;
    }

    YBMineNetWork.mineQiYeAndPersonEditingData(editingMap,success:(data){
      print('---$data-');
      mineUserInfoEventBus.fire(YBMineUserInfoEventBus(info: "1"));
    },fail: (error){
      print("---$error--");
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    mineUserInfoEventBus.destroy();
    imageStrEventBus.destroy();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Container(
       child:Scaffold(
         appBar: AppBar(
           elevation: 0,
           title: Text(
             YBChineseText.minePersonInfoTitle,
             style: YBTextStyle.TextStyle30313320Medium
           ),
         ),
         body:ChangeNotifierProvider(create: (context) => YBMineUserProvider()..getUserInfoData(),
           child: Consumer<YBMineUserProvider>(
               builder:(context,userInfo,child){
                 Widget orginzationWidget;
                 Widget orginzationInfo;
                 if(userInfo.mineUserInfoModel != null){
                   if (userInfo.mineUserInfoModel.orgList.length > 0) {
                     YBMineOrginzationManagerModel managermodel = userInfo.mineUserInfoModel.orgList.first;
                       orginzationInfo = Container(
                         child: Padding(
                           padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
                           child: Column(
                             mainAxisAlignment: MainAxisAlignment.start,
                             crossAxisAlignment: CrossAxisAlignment.stretch,
                             children: [
                               Container(
                                 height: YBTextStyle.adapterSize10,
                               ),
                               Container(
                                 height: YBTextStyle.adapterSize20,
                                 child: Text(
                                   YBChineseText.mineQiYeTitle,
                                   style: YBTextStyle.TextStyle18181814Medium,
                                 ),
                               ),
                               Container(
                                 height: YBTextStyle.adapterSize5,
                               ),
                               Container(
                                 height: YBTextStyle.adapterSize20,
                                 child: Text(
                                   YBChineseText.mineSettingXinXiTitle,
                                   style: YBTextStyle.TextStyle7A7A7A12,
                                 ),
                               ),
                               Container(
                                 height: YBTextStyle.adapterSize10,
                               ),
                             ],
                           ),
                         ),
                       );
                        orginzationWidget = Container(
                         child: InkWell(
                           child: Container(
                             height: YBTextStyle.adapterSize88,
                             color: YBColor.white,
                             child: Padding(
                               padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
                               child: Container(
                                 child: Row(
                                   children: [
                                     Container(
                                       child: Image.asset(YBImageStyle.getImagePath('mine_teamInfo')),
                                     ),
                                     Container(
                                       width: YBTextStyle.adapterSize12,
                                     ),
                                     Expanded(
                                       child: Column(
                                         mainAxisAlignment: MainAxisAlignment.center,
                                         crossAxisAlignment: CrossAxisAlignment.start,
                                         children: [
                                           Container(
                                             child: Text(
                                               managermodel.orgName??"",
                                               style: YBTextStyle.TextStyle2F303316Medium,
                                             ),
                                           ),
                                           Container(
                                             height: YBTextStyle.adapterSize15,
                                           ),
                                           Container(
                                             child: Text(
                                               "${YBChineseText.mineWoZuZhiMangerNameTitle}${managermodel.manageName}"??YBChineseText.mineWoZuZhiMangerNameTitle,
                                               style: YBTextStyle.TextStyle90939912,
                                             ),
                                           ),
                                         ],
                                       ),
                                     ),
                                     Container(
                                       child: Text(
                                         YBChineseText.mineAuthTitle,
                                         style: YBTextStyle.TextStyleA9AEB812,
                                       ),
                                     ),
                                     Container(
                                       width: YBTextStyle.adapterSize5,
                                     ),
                                     Container(
                                       child: Image.asset(YBImageStyle.getImagePath('mine_orgianAuth')),
                                     ),
                                     Container(
                                       width: 8,
                                     ),
                                     Container(
                                       child: Image.asset(YBImageStyle.getImagePath('findIconArrowRight')),
                                     )
                                   ],
                                 ),
                               ),
                             ),
                           ),
                           onTap: (){
                             YBApplication.router.navigateTo(context, '/mine/myUserInfoPage/minemyUserInfoZuzhiGuanLiInfoPage');
                           },
                         ),
                       );
                   }else{
                      orginzationInfo = Container();
                      orginzationWidget = Container();
                   }
                 }

                 mineUserInfoEventBus.on<YBMineUserInfoEventBus>().listen((event) {
                    userInfo.getUserInfoData();
                 });
                  _avatorStr = userInfo.mineUserInfoModel.avatarUrl;
                  _wxQrCode = userInfo.mineUserInfoModel.wxQrCode;
                  print("object-----${_wxQrCode}--");
                 return SingleChildScrollView(
                   child: Column(
                     children: [
                       Container(
                         child: Padding(
                           padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
                           child: Column(
                             mainAxisAlignment: MainAxisAlignment.start,
                             crossAxisAlignment: CrossAxisAlignment.stretch,
                             children: [
                               Container(
                                 height: YBTextStyle.adapterSize10,
                               ),
                               Container(
                                 height: YBTextStyle.adapterSize20,
                                 child: Text(
                                   YBChineseText.minePersonInfoTitle,
                                   style: YBTextStyle.TextStyle18181814Medium,
                                 ),
                               ),
                               Container(
                                 height: YBTextStyle.adapterSize5,
                               ),
                               Container(
                                 height: YBTextStyle.adapterSize20,
                                 child: Text(
                                   YBChineseText.mineEditingInfoTitle,
                                   style: YBTextStyle.TextStyle7A7A7A12,
                                 ),
                               ),
                               Container(
                                 height: YBTextStyle.adapterSize10,
                               ),
                             ],
                           ),
                         ),
                       ),
                       Container(
                         child:InkWell(
                           child: _getAtavUrlLoadInfo(_avatorStr),
                           onTap: () async {
                             setState(() {
                               _wxBool = false;
                             });
                              YBCameraSelectPopPage.getCameraSelectPop(context);
                           },
                         ),
                       ),
                       Container(
                         child:InkWell(
                           child: _getCompanyLoadInfo(YBChineseText.mineNaneNameTitle,userInfo.mineUserInfoModel.nickName??""),
                           onTap: () async {
                             YBApplication.router.navigateTo(context, '/mine/myUserInfoPage/minemyUserInfoEditingPage?titleHeader=${Uri.encodeComponent('昵称')}&&contentTitle=${Uri.encodeComponent(userInfo.mineUserInfoModel.nickName??"")}');
                           },
                         ),
                       ),
                       Container(
                         height: 8,
                       ),
                       Container(
                         child:InkWell(
                           child: _getCompanyLoadInfo(YBChineseText.mineXingMingTitle, userInfo.mineUserInfoModel.contactPerson??""),
                           onTap: () async {
                             YBApplication.router.navigateTo(context, '/mine/myUserInfoPage/minemyUserInfoEditingPage?titleHeader=${Uri.encodeComponent('姓名')}&&contentTitle=${Uri.encodeComponent(userInfo.mineUserInfoModel.contactPerson??"")}');
                           },
                         ),
                       ),
                       Container(
                         child:InkWell(
                           child: _getCompanyLoadInfo(YBChineseText.mineLianXiDianHuaTitle, userInfo.mineUserInfoModel.contactPersonPhone??""),
                           onTap: () async {
                             YBApplication.router.navigateTo(context, '/mine/myUserInfoPage/minemyUserInfoEditingPage?titleHeader=${Uri.encodeComponent('联系电话')}&&contentTitle=${Uri.encodeComponent(userInfo.mineUserInfoModel.contactPersonPhone??"")}');
                           },
                         ),
                       ),
                       Container(
                         child:InkWell(
                           child: _getCompanyLoadInfo(YBChineseText.mineWeChatHaoTitle, userInfo.mineUserInfoModel.wxNumber??""),
                           onTap: () async {
                             YBApplication.router.navigateTo(context, '/mine/myUserInfoPage/minemyUserInfoEditingPage?titleHeader=${Uri.encodeComponent('微信号')}&&contentTitle=${Uri.encodeComponent(userInfo.mineUserInfoModel.wxNumber??"")}');
                           },
                         ),
                       ),
                       Container(
                         child:InkWell(
                           child: _getWeChatQrCodeLoadInfo(_wxQrCode??""),
                           onTap: () async {
                             setState(() {
                               _wxBool = true;
                             });
                             YBCameraSelectPopPage.getCameraSecondSelectPop(context);
                           },
                         ),
                       ),
                       Container(
                         child:InkWell(
                           child: _getCompanyLoadInfo(YBChineseText.mineEmailTitle, userInfo.mineUserInfoModel.email??""),
                           onTap: () async {
                              YBApplication.router.navigateTo(context, '/mine/myUserInfoPage/minemyUserInfoEditingPage?titleHeader=${Uri.encodeComponent('邮箱')}&&contentTitle=${Uri.encodeComponent(userInfo.mineUserInfoModel.email??"")}');
                           },
                         ),
                       ),
                       orginzationInfo,
                       orginzationWidget,
                     ],
                   ),
                 );
               }
           ),
         ),

       ),
    );
  }

  Widget _getCompanyLoadInfo(String title, String bodyTitle){
    return Container(
      color: YBColor.white,
      height: YBTextStyle.adapterSize58,
      child: Padding(
        padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
        child: Row(
          children: [
            Container(
              child: Text(
                title,
                style: YBTextStyle.TextStyle2F303314,
              ),
            ),
            Expanded(
              child: Container(
                alignment: Alignment.centerRight,
                child: Text(
                  bodyTitle,
                  style: YBTextStyle.TextStyle2F303314,
                ),
              ),
            ),
            Container(
              width: YBTextStyle.adapterSize5,
            ),
            Container(
              child: Image.asset(YBImageStyle.getImagePath('findIconArrowRight')),
            ),
          ],
        ),
      ),
    );
  }

  Widget _getAtavUrlLoadInfo(String logoImage){
    return Container(
      color: YBColor.white,
      height: YBTextStyle.adapterSize58,
      child: Padding(
        padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
        child: Row(
          children: [
            Container(
              child: Text(
                YBChineseText.mineAvatolTitle,
                style: YBTextStyle.TextStyle2F303314,
              ),
            ),
            Spacer(),
            Container(
              child: Container(
                height: YBTextStyle.adapterSize40,
                width: YBTextStyle.adapterSize40,
                alignment: Alignment.centerRight,
                decoration: BoxDecoration(
                  color: YBColor.white,
                  borderRadius: BorderRadius.all(Radius.circular(YBTextStyle.adapterSize20)),
                  image: DecorationImage(
                    image: CachedNetworkImageProvider(logoImage??""),
                    fit: BoxFit.cover,
                  )
                ),
              ),
            ),
            Container(
              width: YBTextStyle.adapterSize5,
            ),
            Container(
              child: Image.asset(YBImageStyle.getImagePath('findIconArrowRight')),
            ),
          ],
        ),
      ),
    );
  }

  Widget _getWeChatQrCodeLoadInfo(String logoImage){
    return Container(
      color: YBColor.white,
      height: YBTextStyle.adapterSize58,
      child: Padding(
        padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
        child: Row(
          children: [
            Container(
              child: Text(
                YBChineseText.mineErWeiMaTitle,
                style: YBTextStyle.TextStyle2F303314,
              ),
            ),
            Spacer(),
            Container(
              child: Container(
                height: YBTextStyle.adapterSize40,
                width: YBTextStyle.adapterSize40,
                alignment: Alignment.centerRight,
                decoration: BoxDecoration(
                    color: YBColor.white,
                    borderRadius: BorderRadius.all(Radius.circular(YBTextStyle.adapterSize20)),
                    image: DecorationImage(
                      image: CachedNetworkImageProvider("${YBApi.Base_image_URL}/$logoImage"??""),
                      fit: BoxFit.cover,
                    )
                ),
              ),
            ),
            Container(
              width: YBTextStyle.adapterSize5,
            ),
            Container(
              child: Image.asset(YBImageStyle.getImagePath('findIconArrowRight')),
            ),
          ],
        ),
      ),
    );
  }

}
